package com.qin.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.qin.entity.User;
import com.qin.service.UserService;
import com.qin.service.impl.UserServiceImpl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.log.StaticLog;

/**
 * 支付订单
 */
@WebServlet("/buybook")
public class PaymentOrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private UserService userService = new UserServiceImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 从session域中获取user
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		
		// 从请求域中，拿到订单id
		String orderId = request.getParameter("orderId");
		
		// 如果拿到了，就开始业务处理，如果没拿到，就重定向到首页
		if(!StrUtil.isEmpty(orderId)) {
			// 开始购买图书
			int paymentOrder = userService.paymentOrder(Long.parseLong(orderId),user);
			
			StaticLog.debug("购买图书，影响行数：{}", paymentOrder);
			
			// 成功，放入的值就不是0
			response.getWriter().print(paymentOrder);
		}else {
			response.sendRedirect(request.getContextPath());
		}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
